package ruleta_wicket.ruleta_wicket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import ar.edu.unq.auxiliar.domain.BetModel;
import domain.*;

public class ToBetPanel extends Panel {
	
	private BetModel model;
	private String selected;
	private Object selectedOption;
	private DropDownChoice<String> makes;
	private DropDownChoice<String> models;
	private TablePage home;
	private TextField<Bet> chipsToBet;
	
	public ToBetPanel(String id, Player player, TablePage page) {
		super(id);
		this.model = new BetModel(player);
		this.home = page; 
		
        Form form = new Form("form");
        addActionCombos(form); 
        addActionChip(form);
		addButton(form); 
		
        this.add(form);
	}
	
	public void addActionChip (Form form){
		chipsToBet = new TextField<Bet>("count", new PropertyModel<Bet>(getModel(), Bet.COUNT));
		form.add(chipsToBet);
	}
	
	public void addActionCombos(Form form){
        
        IModel<List<? extends String>> makeChoices = new AbstractReadOnlyModel<List<? extends String>>()
        {
            @Override
            public List<String> getObject()
            {
                return new ArrayList<String>( ToBetPanel.this.getModel().getBets());
            }

        };     

        IModel<List<? extends String>> modelChoices = new AbstractReadOnlyModel<List<? extends String>>()
        {
            @Override
            public List<String> getObject()
            {
                List models = ToBetPanel.this.getModel().getOptions(selected)  ;
                
                if (models == null)
                    models = Collections.emptyList();

                return models;
            }
        };

        this.makes = new DropDownChoice<String>("select", new PropertyModel<String>(this, "selected"), makeChoices);
        
        this.models = new DropDownChoice<String>("models", new PropertyModel(this, "selectedOption"), modelChoices);
        
        this.models.setOutputMarkupId(true);

        form.add(this.makes);
        form.add(this.models);
       
        this.makes.add(update());
	}

	private AjaxFormComponentUpdatingBehavior update() {
		return new AjaxFormComponentUpdatingBehavior("onchange"){
        	@Override
        	protected void onUpdate(AjaxRequestTarget target) {
        		target.addComponent(models);
        	}
        };
	}
	
	
	public void addButton(Form form){   
		form.add(new Button("Apostar") {
			@Override
			public void onSubmit() {
				int valor = Integer.parseInt(chipsToBet.getValue()); 
				ToBetPanel.this.getModel().apostar();
				System.out.println(WicketApplication.getApplication().getTable().getActualBets().size());
			}
		});
	
	}
	
	public TextField<Bet> getChipsToBet() {
		return chipsToBet;
	}
	
	public TablePage getHome() {
		return home;
	}
	
	public BetModel getModel() {
		return model;
	}
	
	public void setModel(BetModel model) {
		this.model = model;
	}
	
	public void setSelected(String selected){
        this.selected = selected;
    }
	
	public String getSelected() {
		return selected;
	}
}
